﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using MvcSoftDemo.Models;
using MvcSoftDemo.Repository;
using MvcSoftDemo.Request;

namespace MvcSoftDemo.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        //index页面初始化
        public IActionResult Index()
        {
            var CategoryRepository = new EfRepository<Category>();
            var categorys = CategoryRepository.Select.FirstOrDefault();
            if (categorys == null)
            {
                CategoryRepository.Insert(new Category
                {
                    CategoryName = "闽大"
                });
            }

            var ProductRepository = new EfRepository<Product>();
            var products = ProductRepository.Select.Where(x => x.IsDeleted == false).ToList();
            return View(products);
        }
        public IActionResult Query(QueryRequest model)
        {
            var ProductRepository = new EfRepository<Product>();
            var products = ProductRepository.Select.Where(x => x.IsDeleted == false).ToList();
            var keyword = model.keyword != null ? model.keyword.Trim() : "";
            if(keyword.Length>0){
                products = ProductRepository.Select
                .Where(x=>x.IsDeleted == false && (x.ProductName.Contains(keyword)))
                .ToList();
            }
            return Json(products);
        }
        public IActionResult CreateOrEdit(int id)
        {
            var ProductRepository = new EfRepository<Product>();
            var products = ProductRepository.GetById(id);
            return View(products);
        }
        public IActionResult Delete(int id)
        {
            var ProductRepository = new EfRepository<Product>();
            ProductRepository.Delete(id);
            return RedirectToAction("Index");
        }
        public IActionResult Save(CreateOrEditRequest model)
        {
            var ProductRepository = new EfRepository<Product>();
            if (model.Id > 0)
            {
                var products = ProductRepository.GetById(model.Id);
                if(products != null){
                    products.ProductName = model.ProductName;
                    products.Supplier = model.Supplier;
                    products.Stock = model.Stock;
                    products.Price = model.Price;
                    products.Remarks = model.Remarks;
                    ProductRepository.Update(products);
                }
            }
            else
            {
                ProductRepository.Insert(new Product
                {
                    ProductName = model.ProductName,
                    Supplier = model.Supplier,
                    Price = model.Price,
                    Stock = model.Stock,
                    Remarks = model.Remarks,
                    CategoryId = 1
                });
            }
            return Ok();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
